package com.yx.modules.api.controller;

import com.yx.common.utils.PageUtils;
import com.yx.common.utils.Query;
import com.yx.common.utils.R;
import com.yx.modules.api.annotation.LoginUser;
import com.yx.modules.api.entity.TbUser;
import com.yx.system.entity.TdMessage;
import com.yx.system.service.TdMessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 消息控制器
 *
 * @author xiaoyu
 */
@RestController
@RequestMapping("/api/message")
public class TdMessageController {

    @Autowired
    private TdMessageService tdMessageService;

    /**
     * 按UID查询消息（包含系统消息）
     */
    @PostMapping("findByUid")
    public R findByuid(@LoginUser TbUser tbUser, @RequestParam Map<String, Object> map) {
        List<TdMessage> mList = new ArrayList<>();
        map.put("uid", tbUser.getUuid());
        Query query = new Query(map);
        mList = tdMessageService.queryList(query);
        int total = tdMessageService.queryTotal(query);
        PageUtils pageUtil = new PageUtils(mList, total, query.getLimit(), query.getPage());
        return R.ok().put("mList", pageUtil);
    }


    /**
     * 按ID查询消息消息详情
     *
     * @param id id
     * @return
     */
    @PostMapping("findByid")
    public R findByid(int id) {
        TdMessage m = tdMessageService.selectByPrimaryKey(id);
        m.setState(1);
        tdMessageService.updateByPrimaryKey(m);
        return R.ok().put("message", m);
    }

    /**
     * 按uid查询用户是否有未读消息
     */
    @PostMapping("findUidByState")
    public R findUidByState(@LoginUser TbUser tbUser, @RequestParam Map<String, Object> map) {
        List<TdMessage> mList = new ArrayList<>();
        map.put("uid", tbUser.getUuid());
        mList = tdMessageService.queryList(map);
        int num = 0;
        for (int i = 0; i < mList.size(); i++) {
            if (mList.get(i).getState() == 0) {
                num++;
            }
        }
        return R.ok().put("num", num);
    }


    /**
     * 按消息类型查询个人消息
     *
     * @return
     */
    @PostMapping("findByType")
    public R findByType(@LoginUser TbUser tbUser, @RequestParam Map<String, Object> map) {
        List<TdMessage> mList = new ArrayList<>();
        map.put("uid", tbUser.getUuid());
        Query query = new Query(map);
        mList = tdMessageService.queryList(query);
        int total = tdMessageService.queryTotal(query);
        PageUtils pageUtil = new PageUtils(mList, total, query.getLimit(), query.getPage());
        return R.ok().put("mList", pageUtil);
    }

    /**
     * 情空个人消息
     *
     * @return
     */
    @PostMapping("cleanmessage")
    public R cleanmessage(@LoginUser TbUser tbUser) {
        Map<String, Object> map = new HashMap<>();
        List<TdMessage> mList = new ArrayList<>();
        map.put("uid", tbUser.getUuid());
        mList = tdMessageService.queryList(map);
        for (int i = 0; i < mList.size(); i++) {
            mList.get(i).setIsdelete(true);
            tdMessageService.updateByPrimaryKey(mList.get(i));
        }
        return R.ok();
    }

}
